#ifndef RESIZEUI_H
#define RESIZEUI_H

#include <QWidget>
#include<QCoreApplication>
#include <QFont>
#include <QObject>
#include <iostream>
#include <vector>
using namespace std;

typedef struct{
    int x,y;
    int w,h;
    int font_size;
    QWidget *subw;
}WidgetInfo;


class ResizeUI: public QObject
{
    Q_OBJECT

public:

    ResizeUI(){

    }

    int width;
    int height;
    //sub widgets information
    vector<WidgetInfo> parts;

public:

    void init(int w,int h)
    {
        width=w;
        height=h;
    }

    void addInfo(QWidget *w)    //initial information
    {
        WidgetInfo info;
        info.subw=w;
        info.x=w->x();
        info.y=w->y();
        info.w=w->width();
        info.h=w->height();
        info.font_size=w->font().pointSize();
        parts.push_back(info);
    }


    void resizes(int w, int h)    //update sub widgets
    {
        double rw=1.0*w/width;
        double rh=1.0*h/height;
        for(int i=0;i<parts.size();i++)
        {
            resizes(parts[i].subw,parts[i],rw,rh);
        }
    }

    void resizes(QWidget *w, WidgetInfo info, double ratioW, double ratioH) //update sub widget
    {
        w->setGeometry(info.x*ratioW,info.y*ratioH,info.w*ratioW,info.h*ratioH);
        QFont font=w->font();
        font.setPointSize(info.font_size*std::min(ratioH,ratioW));
        w->setFont(font);
    }
};

#endif // RESIZEUI_H
